<!-- 房间设置 -->
<template>
	<view class="set-container">
		<u-steps :list="numList" :current="2" mode="number"></u-steps>
		<u-gap height="12" bgColor="#F7F7F7"></u-gap>
		<view class="home-container">
			<view class="scroll-x">
				<view class="list">
					<view class="item" v-for="(item,superIndex) in infoList" :key="superIndex">
						<view class="subTitle">
							{{item.unit}}
						</view>
						<view class="subList">
							<block v-for="(sub,subIndex) in item.floorHouseEntityList" :key="subIndex">
								<block v-if="sub.coordinate.indexOf(',')>0">
									<view class="sub"
										:style="{'width':(subWidth*sub.coordinate.split(',').length)+'vw'}">
										<view class="subName">
											<text>{{sub.houseName}}</text>
											<text>{{sub.houseType}}</text>
											<text>{{sub.houseArea}}{{sub.houseArea ? "m²" : ""}}</text>
										</view>
										<!-- 如果是不能编辑的则不让选 -->
										<block v-if="sub.disabled">
											<view class="clear">
											</view>
										</block>
										<!-- 否则进行判断 isBatch -->
										<block v-else>
											<view class="seContainer" v-show="isBatch"
												@click="handleClick(sub,superIndex)">
												<image src="../../static/images/radio.png" mode="" v-if="!sub.check">
												</image>
												<image src="../../static/images/radio_active.png" mode="" v-else>
												</image>
											</view>
										</block>
									</view>
								</block>

								<view class="sub" :style="{'width':subWidth+'vw'}" v-else>
									<!-- {{sub.disabled}} -->
									<view class="subName">
										<text>{{sub.houseName}}</text>
										<text>{{sub.houseType}}</text>
										<text>{{sub.houseArea}}{{sub.houseArea ? "m²" : ""}}</text>
									</view>
									<!-- 如果是不能编辑的则不让选 -->
									<block v-if="sub.disabled">
										<view class="clear">
										</view>
									</block>
									<!-- 否则进行判断 isBatch -->
									<block v-else>
										<block v-if="isBatch">
											<view class="seContainer" @click="handleClick(sub,superIndex)">
												<image src="../../static/images/radio.png" mode="" v-if="!sub.check">
												</image>
												<image src="../../static/images/radio_active.png" mode="" v-else>
												</image>
											</view>
										</block>
									</block>
								</view>
							</block>
						</view>
					</view>
				</view>
			</view>
		</view>

		<view class="footer">
			<view class="btns" v-if="optionBtnShow">
				<button @click="handleBatch">
					{{isBatch?'取消编辑':'批量编辑'}}
				</button>
				<button @click="sure">完成</button>
			</view>
			<view class="editBtns" v-else>
				<button @click="combineFloor()">合并</button>
				<button @click="splitFloor()">拆分</button>
				<button @click="clearFloor()">清除</button>
				<button @click="next()">确定</button>
			</view>


			<!-- <view class="btn" @click="handleBatch">
				{{isBatch?'取消编辑':'批量编辑'}}
			</view>

			<view class="btn" @click="combineFloor">
				合并
			</view>
			<view class="btn" @click="splitFloor">
				拆分
			</view>

			<view class="btn" @click="clearFloor">
				清除
			</view>
			<view class="btn" @click="sure">
				确定
			</view> -->
		</view>
	</view>
</template>

<script>
	import {
		floorControllerAdd,
		combineFloor,
		addFloor
	} from '@/unit/api.js';
	export default {
		data() {
			return {
				numList: [{
					name: '基本信息'
				}, {
					name: '楼层模板'
				}, {
					name: '房间设置'
				}],
				subWidth: 24,
				infoList: [],
				isBatch: false,
				combineArr: [],
				optionBtnShow: true
			}
		},
		onLoad() {
			this.infoList = uni.getStorageSync("houseList");
			this.infoList = this.infoList.map(v => {
				return {
					...v,
					floorHouseEntityList: v.floorHouseEntityList.map(vv => {
						return {
							...vv,
							check: false,
						}
					})
				}
			})
		},
		methods: {
			//批量操作
			handleBatch() {
				this.isBatch = !this.isBatch;
				this.optionBtnShow = !this.optionBtnShow;
				this.infoList = this.infoList.map(v => {
					return {
						...v,
						floorHouseEntityList: v.floorHouseEntityList.map(vv => {
							return {
								...vv,
								check: false
							}
						})
					}
				})
				this.combineArr = []
			},

			//初始化数据
			initDta() {
				this.combineArr = []; //置空合并的数组列表
				this.isBatch = false; //批量操作false
				//初始化全部未选中状态
				this.infoList = this.infoList.map(v => {
					return {
						...v,
						floorHouseEntityList: v.floorHouseEntityList.map(vv => {
							return {
								...vv,
								check: false,
								disabled: false
							}
						})
					}
				})
			},

			//调用合并接口
			combineFloor() {
				this.infoList.map(v => {
					v.floorHouseEntityList.map((vv, index) => {
						if (vv.check) {
							this.combineArr.push(vv)
						}
					})
				})
				let oldData = []
				oldData = this.countOldData()
				let data = {
					floorHouseMerge: this.combineArr,
					floorHouseOld: oldData,
					floorHouseSplit: {}
				}
				combineFloor(data).then(res => {
					console.log(res, "合并返回")
					uni.showToast({
						title: res.data.msg,
						icon: "none",
						duration: 2000
					})
					if (res.data.code == 1) {
						this.infoList = res.data.object
						uni.setStorageSync("houseList", this.infoList)
						this.initDta();
						this.optionBtnShow = true;
					} else {
						this.initDta()
					}
				})
			},

			//拆分
			splitFloor() {
				let oldData = []
				oldData = this.countOldData(); //获取原始数据

				/**
				 * combineArr表示合并的数组
				 * 拆分也是同理，其实表示的就是选中的
				 * */
				this.infoList.map(v => {
					v.floorHouseEntityList.map((vv, index) => {
						if (vv.check) {
							this.combineArr.push(vv)
						}
					})
				})
				if (this.combineArr.length > 1) {
					uni.showToast({
						title: '只能选中一个进行拆分'
					})
					return;
				}
				let splitData = this.combineArr[0]
				let data = {
					floorHouseMerge: [],
					floorHouseOld: oldData,
					floorHouseSplit: splitData
				}
				combineFloor(data).then(res => {
					uni.showToast({
						title: res.data.msg,
						icon: "none",
						duration: 2000
					})
					if (res.data.code == 1) {
						this.infoList = res.data.object
						uni.setStorageSync("houseList", this.infoList)
						this.initDta()
						this.optionBtnShow = true;
					} else {
						this.initDta()
					}

				})
			},

			//清除楼层
			clearFloor() {
				this.infoList.map(v => {
					v.floorHouseEntityList.map((vv, index) => {
						if (vv.check) {
							this.combineArr.push(vv)
						}
					})
				})
				console.log(this.combineArr, "清除选中的数据")

				//判断一下先只能对一条数据进行清除
				if (this.combineArr.length > 1) {
					uni.showToast({
						title: "只能对单挑数据进行清除",
						icon: "none",
						duration: 2000
					})
					return;
				}

				//获取清除的数据
				let tmp = {}
				tmp = this.combineArr[0]; //暂时只能一条进行操作

				//清除的时候设置当前清除的数据的disabled为true,其他为false
				this.infoList = this.infoList.map(v => {
					return {
						...v,
						floorHouseEntityList: v.floorHouseEntityList.map(vv => {
							//如果数据本来就是禁用的，则啥都不操作，直接返回
							if (vv.disabled) {
								return {
									...vv
								}
							} else {
								return {
									...vv,
									disabled: tmp.houseName == vv.houseName && tmp.coordinate == vv
										.coordinate ? true : false
								}
							}

						})
					}
				})

				this.isBatch = false; //重置批量操作
				this.combineArr = []; //重置选中的值
				this.optionBtnShow = true;

				uni.setStorageSync("houseList", this.infoList)

				/**
				 * 如果最终需要确认提交到后端，
				 * 则用下面方法过滤数据
				 * */

				let oldData = this.countOldData()
			},

			/**
			 * 过滤清除后的数据
			 * */
			countOldData() {
				let tmp = []
				let data = this.infoList
				for (var i = 0; i < data.length; i++) {
					if (data[i]['floorHouseEntityList'] && data[i]['floorHouseEntityList'].length > 0) {
						for (var j = 0; j < data[i]['floorHouseEntityList'].length; j++) {
							//这个判断表示清除后的数据不能放进原始数据中提交
							if (!data[i]['floorHouseEntityList'][j]['disabled']) {
								tmp.push(data[i]['floorHouseEntityList'][j])
							}
						}
					}
				}
				return tmp
			},

			//提交
			sure() {
				// let data = this.infoList
				/***
					如果确定时候传的原始数据需要过滤掉禁用的，用这个方法
				*/
				let oldData = this.countOldData()
				let data = oldData;

				//调用确定接口
				addFloor(data).then(res => {

				})
			},

			//选中
			handleClick(sub, superIndex) {
				if (sub.disabled) {
					uni.showToast({
						title: "该房子已被清除，不能选中",
						icon: "none",
						duration: 2000
					})
					return;
				}
				sub.check = !sub.check
			},
			
			// 下一步
			next() {
				this.optionBtnShow = true;
			}
		}
	}
</script>

<style lang="scss">
	/deep/.u-gap {
		margin-top: 40rpx !important;
		margin-bottom: 10rpx !important;
	}

	.home-container {
		display: flex;
		flex-direction: column;
		padding-left: 10rpx;
		padding-bottom: 100rpx;

		.scroll-x {
			width: 100vw;
			overflow-x: scroll;
		}

		.list {
			display: flex;

			.item {
				display: flex;
				flex-direction: column;
				width: 100%;
				margin-right: 10rpx;

				.subTitle {
					display: flex;
					justify-content: center;
					width: 100vw;
					font-size: 32rpx;
					padding: 10rpx 0;
					margin-bottom: 20rpx;
					margin-top: 20rpx;
					background: #EFFAFF;
				}

				.subList {
					display: flex;
					flex-wrap: wrap;
					// margin-right: -20rpx;
					overflow: hidden;
					justify-content: space-between;

					.sub {
						display: flex;
						flex-direction: column;
						position: relative;
						// width: calc(25vw - 24rpx);
						// width: 24%;
						height: 180rpx;
						border: 1rpx solid #D9D9D9;
						margin-bottom: 20rpx;

						.subName {
							display: flex;
							justify-content: center;
							align-items: center;
							text-align: center;
							flex-wrap: wrap;
							text{
								width: 100%;
								font-size: 24rpx;
							}
						}

						.seContainer {
							display: flex;
							justify-content: center;
							align-items: center;
							position: absolute;
							width: 100%;
							height: 100%;
							z-index: 100;
							background: rgba(0, 0, 0, .3);

							image {
								width: 40rpx;
								height: 40rpx;
							}
						}

						.clear {
							position: absolute;
							width: 100%;
							height: 100%;
							z-index: 100;
							background: rgba(153, 153, 153, .1);
							z-index: 100;
							// background-color: #e2e2e2;
						}
					}
				}
			}
		}
	}

	.footer {
		display: flex;
		align-items: center;
		justify-content: space-around;
		position: fixed;
		height: 100rpx;
		width: 100%;
		left: 0;
		bottom: 10rpx;
		background-color: #FFFFFF;
		z-index: 10000;

		.btns {
			margin: auto;
			margin-top: 50rpx;
			display: flex;
			margin-bottom: 50rpx;
			width: 90%;
			justify-content: space-between;
			display: flex;
		
			button {
				font-size: 32rpx;
		
				&:first-child {
					width: 45%;
					height: 80rpx;
					line-height: 80rpx;
					background: none;
					color: #1A9CEE;
					border: 1px solid #1A9CEE;
				}
		
				&:last-child {
					width: 45%;
					height: 82rpx;
					line-height: 82rpx;
					background: #1A9CEE;
					color: #fff;
				}
			}
		}
		
		.editBtns {
			margin: auto;
			margin-top: 50rpx;
			display: flex;
			margin-bottom: 50rpx;
			width: 90%;
			align-items: center;
			justify-content: space-between;
		
			button {
				background: #1A9CEE;
				height: 80rpx;
				line-height: 80rpx;
				font-size: 32rpx;
				color: #fff;
				width: 200rpx;
				margin-left: 40rpx;
			}
		}
	}

	

	
</style>
